package cn.zhangxin.pethome.system.systemdictionarydetail.controller;

import cn.zhangxin.pethome.system.systemdictionarydetail.domain.Systemdictionarydetail;
import cn.zhangxin.pethome.system.systemdictionarydetail.query.SystemdictionarydetailQuery;
import cn.zhangxin.pethome.system.systemdictionarydetail.service.ISystemdictionarydetailService;
import cn.zhangxin.pethome.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "产品的API",description="产品相关的CRUD功能")
@RequestMapping("/detail")
public class SystemdictionarydetailController {

    @Autowired
    private ISystemdictionarydetailService systemdictionarydetailService;

    @PostMapping("/pagelist")
    @ApiOperation(value = "获取列表" )
    public AjaxResult findByQuery(@RequestBody SystemdictionarydetailQuery query){
        System.out.println(query);
        List<Systemdictionarydetail> byQuery = systemdictionarydetailService.findByQuery(query);
        Long count = systemdictionarydetailService.findByQueryCount(query);
        System.out.println(count);
        return AjaxResult.success().setData(byQuery).setTotal(count).setPid(query.getPid());
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一条数据" )
    public AjaxResult delete(@PathVariable Long id){
        try {
            systemdictionarydetailService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error().setMsg("删除失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "保存" )
    public AjaxResult save(@RequestBody Systemdictionarydetail systemdictionarydetail){
        System.out.println(systemdictionarydetail);
        if(systemdictionarydetail.getId()==null){
            try {
                systemdictionarydetailService.add(systemdictionarydetail);
                return AjaxResult.success().setMsg("添加成功");
            }catch (Exception e){
                e.printStackTrace();
            }
            return AjaxResult.error().setMsg("添加失败");
        }else {
            try {
                systemdictionarydetailService.update(systemdictionarydetail);
                return AjaxResult.success().setMsg("修改成功");
            }catch (Exception e){
                e.printStackTrace();
            }
            return AjaxResult.error().setMsg("修改失败");
        }
    }


}
